package com.ruoyi.project.mobile.user;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.thymeleaf.expression.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.customer.customer.domain.Customer;
import com.ruoyi.project.system.customer.customer.service.ICustomerService;
import com.ruoyi.project.system.feedback.domain.Feedback;
import com.ruoyi.project.system.feedback.service.IFeedbackService;
import com.ruoyi.project.system.goods.follow.dto.GoodsFollowDto;
import com.ruoyi.project.system.goods.follow.service.IGoodsFollowService;
import com.ruoyi.project.system.goods.goods.service.IGoodsGoodsService;
import com.ruoyi.project.system.house.house.domain.House;
import com.ruoyi.project.system.house.house.enums.HouseDto;
import com.ruoyi.project.system.house.house.service.IHouseService;
import com.ruoyi.project.system.house.img.domain.HouseImg;
import com.ruoyi.project.system.house.img.service.IHouseImgService;
import com.ruoyi.project.system.house.member.service.IMemberHouseService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.enums.MemberExamine;
import com.ruoyi.project.system.member.enums.MemberIsVip;
import com.ruoyi.project.system.member.service.IMemberService;
import com.ruoyi.project.system.order.order.enums.OrderStatus;
import com.ruoyi.project.system.order.order.service.IOrderOrderService;
import com.ruoyi.project.system.shopconfig.service.IShopConfigService;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/24 16:25
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/user")
public class ApiUserController extends BaseController {

	@Autowired
	private IMemberService memberService;

	@Autowired
	private IOrderOrderService orderOrderService;

	@Autowired
	private IGoodsGoodsService goodsGoodsService;

	@Autowired
	private IGoodsFollowService goodsFollowService;
	@Autowired
	private ServerConfig serverConfig;
	@Autowired
	private IFeedbackService feedbackService;
	@Autowired
	private IShopConfigService shopConfigService;
	@Autowired
	private IMemberHouseService memberHouseService;
	@Autowired
	private IHouseImgService houseImgService;

	@Autowired
	private ICustomerService customerService;

	@Autowired
	private IHouseService houseService;

	/**
	 * 点击会员中心 查询用户信息
	 * @param uid
	 * @return
	 */
	@GetMapping
	public R findUser(@RequestParam String uid){
		HashMap<String, Object> map = new HashMap<>();

		Member oldMember = this.memberService.selectMemberById(uid);


		if(oldMember==null){
			return R.ok();
		}


		map.put("user",oldMember);
		return RFactory.generateR(map);
	}

	/**
	 * 查询关注列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/follows")
	public R userFollows(@RequestParam String uid){
		startPage();
		List<GoodsFollowDto> goodsFollowDtoList = this.goodsFollowService.findListByUid(uid);
		return RFactory.generateR(getDataTable(goodsFollowDtoList));
	}

	/**
	 * 查询用户详细信息
	 * @param uid
	 * @return
	 */
	@GetMapping("/info")
	public R userInfo(@RequestParam String uid){
		return RFactory.generateR(this.memberService.selectMemberById(uid));
	}



	/**
	 * 修改用户信息
	 * @param map
	 * @return
	 */
	@PutMapping("/info")
	public R updateUserInfo(@RequestBody Map map){
		String uid = (String) map.get("uid");
		String name = (String) map.get("name");
		String idCard = (String) map.get("idCard");
		String mobile = (String) map.get("mobile");
		String header = (String) map.get("headerImg");
		String back = (String) map.get("backImg");
		Member member = new Member();
		member.setId(uid);
		member.setName(name);
		member.setIdCard(idCard);
		member.setMobile(mobile);
		member.setIsExamine(MemberExamine.ADOPT.getValue());
		member.setCardHeaderImg(header);
		//上传身份证背面的图片
		member.setCardBackImg(back);
		this.memberService.updateMember(member);
		return R.ok();
	}


	/**
	 * 查询推荐人列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/recommend")
	public R userRecommend(@RequestParam String uid){
		startPage();
		Member member = new Member();
		member.setIntroUser(uid);
		List<Member> members = this.memberService.selectMemberList(member);
		return RFactory.generateR(getDataTable(members));
	}

	/**
	 * 修改用户信息
	 * @param map
	 * @return
	 */
	@PutMapping("/info2")
	public R updateUserInfo2(@RequestBody Map map){
		String username = (String) map.get("username");
		String id = (String) map.get("id");
		int sex = (int) map.get("sex");
		String headpic = (String) map.get("headpic");
		Member member = new Member();
		member.setId(id);
		member.setUsername(username);
		member.setSex(sex);
		member.setHeadpic(headpic);
		this.memberService.updateMember(member);
		return R.ok();
	}


	/**
	 * 提交意见
	 * @param feedback
	 * @return
	 */
	@PostMapping("/feedback")
	public R userFeedback(@RequestBody Feedback feedback){
		feedback.setCreated(DateUtils.getNowDate());
		this.feedbackService.insertFeedback(feedback);
		return R.ok();
	}

	@GetMapping("/share")
	public R share(@RequestParam String uid){
		HashMap<String, Object> map = new HashMap<>();
		Member member = this.memberService.selectMemberById(uid);
		if(StringUtils.isBlank(member.getQrCode())){
			String qrCode = WeixinUtils.getQrCodeImg(member);
			member.setQrCode(qrCode);
			Member update = new Member();
			update.setQrCode(qrCode);
			update.setId(uid);
			this.memberService.updateMember(update);
		}
		map.put("avator",member.getHeadpic());
		map.put("name",member.getUsername());
		map.put("InvitationCode","");
		map.put("shopId","");
		map.put("imgurl",java.util.Arrays.asList(this.shopConfigService.findAll().get(0).getShareImg()));
		map.put("qrCode",member.getQrCode());
		map.put("mobile",member.getMobile());
		return RFactory.generateR(map);
	}


	/**
	 * 提交用户身份证信息
	 * @param member
	 * @return
	 */
	@PostMapping("/idcard")
	public R commitIdCard(@RequestBody Member member){
		//查询用户信息
		Member oldMember = this.memberService.selectMemberById(member.getId());
		if(oldMember.getIsExamine()==2){
			return R.error("已审核通过,不可修改");
		}
		//修改用户信息
		member.setIsExamine(1);
		member.setReason("");
		this.memberService.updateMember(member);
		return R.ok();
	}

	/**
	 * 查询用户收藏列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/collection")
	public R findMemberCollection(@RequestParam String uid){
		startPage();
		List<HouseDto> houseDtos = this.memberHouseService.findListByUid(uid);

		for(HouseDto houseDto:houseDtos){
			//设置图片
			HouseImg houseImg = new HouseImg();
			houseImg.setHouseId(houseDto.getId());
			List<HouseImg> houseImgs = this.houseImgService.selectHouseImgList(houseImg);
			houseDto.setHouseImgs(houseImgs.isEmpty()?null: java.util.Arrays.asList(houseImgs.get(0)));
		}
		return RFactory.generateR(getDataTable(houseDtos));
	}

	/**
	 * 查询客户列表
	 * @param uid
	 * @param status
	 * @return
	 */
	@GetMapping("/customer")
	public R findCustomerList(@RequestParam String uid,
							  @RequestParam(required = false) Integer status){
		startPage();
		Customer customer = new Customer();
		customer.setStatus(status);
		customer.setMemberId(uid);
		List<Customer> customers = this.customerService.selectCustomerList(customer);
		return RFactory.generateR(getDataTable(customers));
	}


	/**
	 * 查询房源列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/house")
	public R findHouseList(@RequestParam String uid){
		startPage();
		House house = new House();
		house.setMemberId(uid);
		List<HouseDto> houseDtos = this.houseService.selectHouseList(house);
		for(HouseDto houseDto:houseDtos){
			//设置图片
			HouseImg houseImg = new HouseImg();
			houseImg.setHouseId(houseDto.getId());
			List<HouseImg> houseImgs = this.houseImgService.selectHouseImgList(houseImg);
			houseDto.setHouseImgs(houseImgs.isEmpty()?null: java.util.Arrays.asList(houseImgs.get(0)));
		}
		return RFactory.generateR(getDataTable(houseDtos));
	}

}
